package com.tian.scroll.pull.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.view.NestedScrollingChild;
import androidx.core.view.NestedScrollingChildHelper;
import androidx.core.view.NestedScrollingParent;
import androidx.core.view.NestedScrollingParentHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.tian.scroll.R;

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/8 14:23
 */
public class NestedScrollLinearLayout extends LinearLayout implements NestedScrollingParent, NestedScrollingChild {

    public static final String TAG = "NestedScrollParent";

    private NestedScrollingParentHelper nestedScrollingParentHelper;
    private NestedScrollingChildHelper nestedScrollingChildHelper;

    private Context mContext;
    private RecyclerView recyclerView;
    private View headerView;
    private int headerHeight;



    public NestedScrollLinearLayout(Context context) {
        super(context);
    }

    public NestedScrollLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NestedScrollLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public NestedScrollingParentHelper getNestedScrollingParentHelper() {
        if (nestedScrollingParentHelper == null) {
            nestedScrollingParentHelper = new NestedScrollingParentHelper(this);
        }
        return nestedScrollingParentHelper;
    }

    public NestedScrollingChildHelper getNestedScrollingChildHelper() {
        if (nestedScrollingChildHelper == null) {
            nestedScrollingChildHelper = new NestedScrollingChildHelper(this);
            nestedScrollingChildHelper.setNestedScrollingEnabled(true);
        }
        return nestedScrollingChildHelper;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        recyclerView = findViewById(R.id.recycler_view);
        headerView = findViewById(R.id.rv_header);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LayoutParams params = (LayoutParams) recyclerView.getLayoutParams();
        params.height = MeasureSpec.getSize(heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        headerHeight = headerView.getMeasuredHeight();
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return true;
    }


    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        getNestedScrollingParentHelper().onNestedScrollAccepted(child, target, axes);
    }


    @Override
    public void onStopNestedScroll(View child) {
        getNestedScrollingParentHelper().onStopNestedScroll(child);
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
       // super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        Log.i("tiansongtao", "onNestedScroll dyUnconsumed=" + dyUnconsumed);
        if (dyUnconsumed < 0) {
            if (getScrollY() + dyUnconsumed > 0) {
                scrollBy(0, dyUnconsumed);
            } else {
                int scrollViewNeedScrollY = getScrollY() + dyUnconsumed;
                scrollBy(0, scrollViewNeedScrollY);
            }
        }
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        //super.onNestedPreScroll(target, dx, dy, consumed);
        Log.i("tiansongtao", "onNestedPreScroll dy=" + dy + "  scrolly=" + getScrollY());
//        if (dy < 0) {
//            //下滑
//        } else {
//            //上滑
            if (getScrollY() < headerHeight) {
                if (getScrollY() + dy < headerHeight) {
                    scrollBy(0, dy);
                    consumed[1] = dy;
                } else if (getScrollY() + dy > headerHeight) {
                    int scrollViewNeedScrollY = headerHeight - getScrollY();
                    scrollBy(0, scrollViewNeedScrollY);
                    consumed[1] = scrollViewNeedScrollY;
                }
            }
       // }
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        //return super.onNestedFling(target, velocityX, velocityY, consumed);
        return false;
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        //return super.onNestedPreFling(target, velocityX, velocityY);
        return false;
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return getNestedScrollingChildHelper().startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        super.stopNestedScroll();
        getNestedScrollingChildHelper().stopNestedScroll();
    }


    @Override
    public boolean hasNestedScrollingParent() {
        return getNestedScrollingChildHelper().hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow) {
        return getNestedScrollingChildHelper().dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow) {
        return getNestedScrollingChildHelper().dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return getNestedScrollingChildHelper().dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return getNestedScrollingChildHelper().dispatchNestedPreFling(velocityX, velocityY);
    }
}
